<?php
namespace Home\Controller;
use Think\Controller;
class ExpenditureController extends Controller {
	
	public function __construct() {
		parent::__construct();
		$ti = M('ti') -> where("user_id = '".$_SESSION['user']['id']."'") -> find();
		
		if(empty($ti) || empty($_SESSION['user']['id']))
		{
			$this->redirect('Home/Login/login');
		}
    }
	
    public function index(){
		
		$pageNo=empty($_REQUEST['pageNo'])?1:$_REQUEST['pageNo'];
		$pageSize=empty($_REQUEST['pageSize'])?10:$_REQUEST['pageSize'];
		$starttime=empty($_REQUEST['starttime'])?'':trim($_REQUEST['starttime']);
		$endtime=empty($_REQUEST['endtime'])?'':trim($_REQUEST['endtime']);
		$name=empty($_REQUEST['name'])?'id':$_REQUEST['name'];
		$asc=empty($_REQUEST['asc'])?'desc':trim($_REQUEST['asc']);
		
		$expenditure=D('expenditure');
		$res=$expenditure->expenditure_list($pageNo,$pageSize,$name,$asc,$starttime,$endtime);
		
		$res_total=$expenditure->expenditure_list_total($name,$asc,$starttime,$endtime);
		
		$this -> assign('user_type',$_SESSION['user']['type']);
		$this->assign('expenditure_list',$res['expenditure_list']);
		$this->assign('count',$res['count']);
		$this->assign('statistics',$res['statistics']);
		$this->assign('statistics_total',$res_total['statistics']);
		$this->assign('data',$res['data']);
		$this->display();
    }
	
	
    public function expenditure_info(){
		$visitor_id = $_SESSION['user']['id'];   
		$id=empty($_REQUEST['id'])?0:$_REQUEST['id'];
	
		$drop_down =D('drop_down'); 
		$expenditure =D('expenditure'); 
		$shouquan = M('shouquan') -> field('group_id') -> where("visitor_id = '$visitor_id'") -> find();
		if($shouquan['group_id']){
			 $shouquan2 = M('shouquan') -> field('visitor_id') -> where("group_id = '".$shouquan['group_id']."'") -> select(); 	
			 foreach($shouquan2 as $k=> $val){
				 if($k == '0'){
					 $s_str = "'".$val['visitor_id']."'";
				 }else{
					 $s_str .= ","."'".$val['visitor_id']."'";
				 }
			 }
			 $where.= "visitor_id in($s_str)";
		 }else{
			 $where .= "visitor_id = '$visitor_id'";
		 }	
		
		$shop_list=M('shop') -> where($where)->field("id,shop_name")->select();
		$pay_list=$drop_down->dropdown_list('pay_method');
		$payee_list=$drop_down->dropdown_list('payee');
		$purpose_list=$drop_down->dropdown_list('purpose');
		//$user_info=$customer->user_info(); 
		$expenditureinfo=$expenditure->get_expenditure_info($id);
		
		if($expenditureinfo)
		{
			$user=M('user_list');
			$userinfo=$user->field('username')->where("id=".$expenditureinfo['user_id'])->find();
			$expenditureinfo['username']=$userinfo['username'];
		}
		
		$this -> assign('user_type',$_SESSION['user']['type']);
		$this->assign("expenditure",$expenditureinfo);
		$this->assign("shop_list",$shop_list);
		$this->assign("pay_list",$pay_list);
		$this->assign("payee_list",$payee_list);
		$this->assign("purpose_list",$purpose_list);
		$this->assign("user_info",$user_info);
		$this->assign("jumpurl",$_SERVER['HTTP_REFERER']);
		$this->display();
    }
	
	public function expenditure_add(){
		$visitor_id = $_SESSION['user']['id']; 
		$expenditure =M('expenditure'); 
		$data=array();//接收的数据 
		$id=empty($_POST['id'])?0:$_POST['id'];
		$username=empty($_POST['username'])?'':$_POST['username'];
		
		$user=M('user_list');
		$userinfo=$user->field('id')->where("visitor_id = '$visitor_id' and username='".$username."'")->find();
		
		$data['user_id']=empty($userinfo)?0:$userinfo['id'];
		$data['expenditure_time']=empty($_POST['expenditure_time'])?0:$_POST['expenditure_time'];
		$data['expenditure_desc']=empty($_POST['expenditure_desc'])?'':trim($_POST['expenditure_desc']);
		$data['purpose']=empty($_POST['purpose'])?'':trim($_POST['purpose']);
		$data['customer_group']=empty($_POST['customer_group'])?0:$_POST['customer_group'];
		$data['pay_method']=empty($_POST['pay_method'])?0:$_POST['pay_method'];
		$data['payee']=empty($_POST['payee'])?'':trim($_POST['payee']);
		$data['payee2']=empty($_POST['payee2'])?'':trim($_POST['payee2']);
		$data['amount']=empty($_POST['amount'])?'':trim($_POST['amount']);
		$data['unit_price']=empty($_POST['unit_price'])?'':trim($_POST['unit_price']);
		$data['num']=empty($_POST['num'])?0:trim($_POST['num']);
		$data['floating_capital']=empty($_POST['floating_capital'])?0:trim($_POST['floating_capital']);
		$data['total']=$data['floating_capital']+$data['amount'];
		$data['remarks']=empty($_POST['remarks'])?'':trim($_POST['remarks']);
		$data['modify_time']=date('Y-m-d H:i:s');
		$data['visitor_id']	 = $_SESSION['user']['id'];
		$visitor_id = $_SESSION['user']['id'];
		$jumpurl=empty($_POST['jumpurl'])?'':trim($_POST['jumpurl']);
		
		if(empty($id))
		{
			$data['add_time']=date('Y-m-d H:i:s');
			$res=$expenditure->add($data);
			
			header('Location:index');
		}
		else
		{
			$res=$expenditure->where("id='$id' and visitor_id = '$visitor_id'")->save($data);
			
			header('Location:'.$jumpurl);
		}
		
		
	}
	
	public function expenditure_upload(){
		 if($_FILES['uploadfile']['error']>0)
		 {
			 $this->error($_FILES['uploadfile']['error']);
		 }
		 else
		 {  
		    $filename=iconv("UTF-8","GB2312",$_FILES['uploadfile']['name']) ;
			$mov= move_uploaded_file($_FILES["uploadfile"]["tmp_name"], "Public/upload/".$filename);
			
			if($mov)
			{
				$data['wangwang']="upload/" . $_FILES["uploadfile"]["name"];
				$id=empty($_POST['id'])?0:$_POST['id'];
				$visitor_id = $_SESSION['user']['id'];
				$where = "id='$id'";
				$shouquan = M('shouquan') -> field('group_id') -> where("visitor_id = '$visitor_id'") -> find();
				if($shouquan['group_id']){
					$shouquan2 = M('shouquan') -> field('visitor_id') -> where("group_id = '".$shouquan['group_id']."'") -> select(); 	
					foreach($shouquan2 as $k=> $val){
						if($k == '0'){
							$s_str = "'".$val['visitor_id']."'";
						}else{
							$s_str .= ","."'".$val['visitor_id']."'";
						}
					}
					$where.= " and visitor_id in($s_str)";
				}else{
					$where .= " and visitor_id = '$visitor_id'";
				}
				$expenditure=M('expenditure');
				
				$res=$expenditure->where("$where")->save($data);
				$this->redirect();
			}
		 }
	}
	
	public function expenditure_export()
	{
		$starttime=empty($_REQUEST['starttime'])?'':trim($_REQUEST['starttime']);
		$endtime=empty($_REQUEST['endtime'])?'':trim($_REQUEST['endtime']);
		$name=empty($_REQUEST['name'])?'id':$_REQUEST['name'];
		$asc=empty($_REQUEST['asc'])?'asc':trim($_REQUEST['asc']);
		
		$expenditure=D('expenditure');
		$res_total=$expenditure->expenditure_list_total($name,$asc,$starttime,$endtime);

		$arr['id']='id';
		$arr['user_id']='经手人';
		$arr['expenditure_time']='支出时间';
		$arr['expenditure_desc']='支出项目说明';
		$arr['purpose']='用途';
		$arr['customer_group']='部门';
		$arr['payee']='收款人';
		$arr['amount']='金额';
		$arr['unit_price']='佣金价格';
		$arr['num']='佣金数量';
		$arr['floating_capital']='流动资金';
		$arr['total']='合计';
		$arr['remarks']='备注';
		$arr['wangwang']='放单旺旺';
		$arr['modify_time']='修改时间';
		$arr['add_time']='添加时间';
		
		$content= implode("\t", array_keys($arr)) . "\n";
        $content.= implode("\t", $arr) . "\n";
		
		foreach($res_total['expenditure_total'] as $v)
		{    
			$data['id']=$v['id'];
			$data['user_id']=$v['username'];
			$data['expenditure_time']=$v['expenditure_time'];
			$data['expenditure_desc']=$v['expenditure_desc'];
			$data['purpose']=$v['purpose'];
			$data['customer_group']=$v['customer_group'];
			$data['payee']=$v['payee'];
			$data['amount']=$v['amount'];
			$data['unit_price']=$v['unit_price'];
			$data['num']=$v['num'];
			$data['floating_capital']=$v['floating_capital'];
			$data['total']=$v['total'];
			$data['remarks']=$v['remarks'];
			$data['wangwang']=$v['wangwang'];
			$data['modify_time']=$v['modify_time'];
			$data['add_time']=$v['add_time'];
		
			$content .= implode("\t", $data) . "\n";
		}
		$statistics="合计金额\t".$res_total['statistics']['amount']."\n";
		$statistics.="合计本金\t".$res_total['statistics']['floating_capital']."\n";
		$statistics.="合计单数\t".$res_total['statistics']['num']."\n";
		$content .="\n\n". $statistics . "\n";
		
		
		$filename="支出数据".date('YmdHis').".xls";
		header("Content-Disposition: attachment; filename=".$filename);
		header("Content-Type: application/unknown");
		echo $content;
	}
	
	public function expenditure_delete(){
		$visitor_id = $_SESSION['user']['id'];  
		$id=empty($_REQUEST['id'])?0:$_REQUEST['id'];
		$where = "id='$id'";
		$shouquan = M('shouquan') -> field('group_id') -> where("visitor_id = '$visitor_id'") -> find();
		if($shouquan['group_id']){
			$shouquan2 = M('shouquan') -> field('visitor_id') -> where("group_id = '".$shouquan['group_id']."'") -> select(); 	
			foreach($shouquan2 as $k=> $val){
				if($k == '0'){
					$s_str = "'".$val['visitor_id']."'";
				}else{
					$s_str .= ","."'".$val['visitor_id']."'";
				}
			}
			$where.= " and visitor_id in($s_str)";
		}else{
			$where .= " and visitor_id = '$visitor_id'";
		}
		$expenditure=M('expenditure');
		$res=$expenditure->where($where)->delete();
		
		header("Location:".$_SERVER['HTTP_REFERER']);
    }
	
	
	
}
















